package com.tansun.easycare.rule.rulelog.service.impl;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.rulelog.dao.RuleLogsDao;
import com.tansun.easycare.rule.rulelog.entity.RuleLogDetails;
import com.tansun.easycare.rule.rulelog.entity.RuleLogs;
import com.tansun.easycare.rule.rulelog.model.FlareTreeDataEntity;
import com.tansun.easycare.rule.rulelog.service.RuleLogDetailsService;
import com.tansun.easycare.rule.rulelog.service.RuleLogsService;

@Service("ruleLogsService")
public class RuleLogsServiceImpl extends RuleBaseServiceImpl<RuleLogsDao, RuleLogs> implements RuleLogsService {
	    /*详细数据的方法*/
	    @Override
	    public PageInfo<RuleLogs> RulefindList(Page<RuleLogs> page, RuleLogs ruleLogs) {
	    	List<RuleLogs> ruleLogsList = this.dao.findList(ruleLogs);
	    	PageInfo<RuleLogs> pageInfo = new PageInfo<>(ruleLogsList);
	        return pageInfo; 
	    }
	    
	    /*group_by的方法*/
	    @Override
	    public PageInfo<RuleLogs> RulegroupList(Page<RuleLogs> page, RuleLogs ruleLogs) {
	
	    	PageInfo<RuleLogs> pageInfo = new PageInfo<>(this.dao.groupfindList(ruleLogs));
    	    return pageInfo;
	    }
	    /*获取饼状图的方法*/
		@Override
		public List<RuleLogs> groupfindListPie(RuleLogs ruleLogs) {
			return this.dao.groupfindListPie(ruleLogs);
		}
		/*点击（切换）的方法*/
		@Override
		public List<RuleLogs> findListSwithPie(RuleLogs ruleLogs) {
			return this.dao.groupfindList(ruleLogs);
		}
		@Autowired
		RuleLogDetailsService ruleLogDetailsService;
		/**
		 * 获取总览树数据
		 * @return
		 */
		public FlareTreeDataEntity getFlareTreeData(String logType) {
			FlareTreeDataEntity entity = new FlareTreeDataEntity();
			entity.setName("规则引擎");
			entity.setChildren(new ArrayList<FlareTreeDataEntity>());
			
			RuleLogs logcondition = new RuleLogs();
			logcondition.setLogType(logType);
			List<RuleLogs> projects = this.dao.findUsedProjects(logcondition);
			List<RuleLogs> logs = this.dao.findList(logcondition);
			RuleLogDetails condition = new RuleLogDetails();
			condition.setLogType(logType);
			List<RuleLogDetails> list = ruleLogDetailsService.findUsedRuleCount(condition);
			Map<String,List<RuleLogDetails>> detailMap = new HashMap<>();
			for (RuleLogDetails ruleLogDetails : list) {
				List<RuleLogDetails> details = detailMap.get(ruleLogDetails.getProjectCode()+ruleLogDetails.getField1());
				if(details==null) {
					details = new ArrayList<RuleLogDetails>();
					detailMap.put(ruleLogDetails.getProjectCode()+ruleLogDetails.getField1(),details);
				}
				details.add(ruleLogDetails);
			}
			
			Map<String,List<RuleLogs>> rl = new HashMap<String,List<RuleLogs>>();
			for (RuleLogs ruleLogs : logs) {
				List<RuleLogs> maplist = rl.get(ruleLogs.getProjectCode()+ruleLogs.getRuleflowid());
				if(maplist==null) {
					maplist = new ArrayList<RuleLogs>();
					rl.put(ruleLogs.getProjectCode()+ruleLogs.getRuleflowid(), maplist);
				}
				maplist.add(ruleLogs);
			}
	           // 创建一个数值格式化对象  
            NumberFormat numberFormat = NumberFormat.getInstance();  
            // 设置精确到小数点后2位  
            numberFormat.setMaximumFractionDigits(2); 
			for (RuleLogs project : projects) {//规则项目
				FlareTreeDataEntity proItem = new FlareTreeDataEntity();
				proItem.setName(project.getProjectName());
				proItem.setChildren(new ArrayList<FlareTreeDataEntity>());
				entity.getChildren().add(proItem);
				for(String key:rl.keySet()) {//规则流
					if(key.indexOf(project.getProjectCode())>-1) {
						RuleLogs log = rl.get(key).get(0);
						FlareTreeDataEntity logItem = new FlareTreeDataEntity();
						logItem.setName(log.getModelName()+log.getVersion()+",共"+rl.get(key).size()+"笔,占:"+(numberFormat.format((float) rl.get(key).size() / (float) logs.size() * 100))+"%");
						logItem.setChildren(new ArrayList<FlareTreeDataEntity>());
						proItem.getChildren().add(logItem);
						List<RuleLogDetails> details = detailMap.get(log.getProjectCode()+log.getRuleflowid());
						if(details!=null) {
						for (RuleLogDetails ruleLogDetails : details) {
							FlareTreeDataEntity detailItem = new FlareTreeDataEntity();
							detailItem.setName(ruleLogDetails.getModelName()+ruleLogDetails.getCount()+"笔");
							detailItem.setValue(ruleLogDetails.getCount());
							logItem.getChildren().add(detailItem);
						}
						}
					}
				}
			}
			return entity;
		}
		
		@Override
		public RuleLogs getStartTimeFlag() {
			return this.dao.getStartTimeFlag();
		}
		
		@Override
		public List<RuleLogs> findListEDNQ(RuleLogs ruleLogs) {
			return this.dao.findListEDNQ(ruleLogs);
		}
}